<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class LM_Table_Playermap extends LM_Table
{
    /** @var int Primary Key */
    public $playermap_id;
    /** @var int references LM_Table_Player */
    public $playermap_player;
    /** @var int references LM_Table_Team */
    public $playermap_team;
    /** @var int references LM_Table_Project */
    public $playermap_project;
    /** @var string image */
    public $playermap_image;
    /** @var int references LM_Table_Position */
    public $playermap_position;
    /** @var int position number */
    public $position_number;

    /*
      public $cnt_matches;
      public $cnt_in;
      public $cnt_out;
      public $cnt_goals;
      public $cnt_own_goals;
      public $cnt_assists;
      public $cnt_yellow;
      public $cnt_yellow_red;
      public $cnt_red;
      public $cnt_penalties;
     */

    /**
     * constructor
     * @param object $db mosDatabase
     */
    public function __construct($db)
    {
        parent::__construct('#__leagueman_playermap', 'playermap_id', $db);
    }

    public function getID()
    {
        return $this->playermap_id;
    }

    public function getCount($field)
    {
        return $this->$field;
    }

    public function getPosition()
    {
        return $this->playermap_position;
    }

    public function getNumber()
    {
        return $this->position_number;
    }

    public function getImage()
    {
        return $this->playermap_image;
    }

    /**
     * function filterbyProject
     * @param int $project_id key of LM_Table_Project
     */
    public function filterbyProject($project_id)
    {
        $this->addFilter("pm.playermap_project = " . $this->_db->Quote(intval($project_id)));
    }

    /**
     * function filterbyTeam
     * @param int $team_id key of LM_Table_Team
     */
    public function filterbyTeam($team_id)
    {
        $this->addFilter("pm.playermap_team = " . $this->_db->Quote(intval($team_id)));
    }

    /**
     * function filterbyPlayer
     * @param int $player_id key of LM_Table_Player
     */
    public function filterbyPlayer($player_id)
    {
        $this->addFilter("pm.playermap_player = " . $this->_db->Quote(intval($player_id)));
    }

    /**
     * function filterbyPosition
     * @param int $position_id key of LM_Table_Position
     * @return mixed 
     */
    public function filterbyPosition($position_id)
    {
        $this->addFilter("pm.playermap_position = " . $this->_db->Quote(intval($position_id)));
    }

    /**
     * function getRows
     * @param int $limitstart
     * @param int $limit
     * @return mixed 
     */
    public function getRows($field = "", $limitstart = 0, $limit = 10)
    {

        $limitstart = intval($limitstart);
        $limit = intval($limit);

        $limitation = ($limit > 0) ? "LIMIT $limitstart, $limit" : "";

        $sql = "SELECT p.*, pm.*, po.*
				  FROM #__leagueman_playermap pm
			INNER JOIN #__leagueman_player p ON p.player_id = pm.playermap_player
			 LEFT JOIN #__leagueman_position po ON po.position_id = pm.playermap_position"
                . $this->getFilter() . "
			  ORDER BY pm.playermap_position, pm.position_number $limitation";
        $this->_db->setQuery($sql);

        return $this->_db->loadObjectList($field);
    }

    /**
     * function getTotalRows
     * @return int total 
     */
    public function getTotalRows()
    {
        $sql = "SELECT COUNT(pm.playermap_id) FROM #__leagueman_playermap pm
			INNER JOIN #__leagueman_player p ON p.player_id = pm.playermap_player
			 LEFT JOIN #__leagueman_position po ON po.position_id = pm.playermap_position"
                . $this->getFilter();
        $this->_db->setQuery($sql);
        return $this->_db->loadResult();
    }

}